package com.zhiyou100.controller.admin;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Speaker;
import com.zhiyou100.service.SpeakerService;

@Controller
@RequestMapping("/admin/speaker")
public class AdminSpeakerController {
	@Autowired
	private SpeakerService ss;
	@RequestMapping("/manage")
	public String manage(Integer pageNum,HttpSession session,Model mo,Speaker speaker) {
		int pageSize = 6;
		Integer p = (Integer) session.getAttribute("pages");
		if(pageNum==null) {
			pageNum=1;

		}
		if(pageNum<1) {
			pageNum=1;
		}
		if(p!=null) {
			if(pageNum>p) {
				pageNum = p;
			}
		}
		PageInfo<Speaker> speakers = null;
		if(speaker!=null) {
			speakers = ss.selectSpeakerByContent(pageNum,pageSize,speaker);
		}else {
			speakers = ss.selectSpeakerPage(pageNum,pageSize);	
		}
		session.setAttribute("pages", speakers.getPages());
		session.setAttribute("pageNum", speakers.getPageNum());
		mo.addAttribute("speakers", speakers.getList());
		mo.addAttribute("s", speaker);
		return "forward:/back/speaker/speaker.jsp";
	}
	
	@RequestMapping("/editSpeaker")
	public String editSpeaker(Integer id,Model mo,HttpSession session) {
		Speaker speaker = ss.selectSpeakers(id);
		mo.addAttribute("speaker", speaker);
		Date date = new Date();
		long time = date.getTime();
		session.setAttribute("time", time);
		return "forward:/back/speaker/edit_speaker.jsp";
	}
	@RequestMapping("/updateSpeaker")
	public String updateSpeaker(Speaker speaker,HttpSession session,String randomCode) {
		Integer  pageNum = (Integer) session.getAttribute("pageNum");
		Long time = (Long) session.getAttribute("time");
		if(time==null) {
			return "redirect:/admin/course/manage?pageNum="+pageNum;
		}
		if(time==Long.parseLong(randomCode)) {
			int i = ss.updateSpeaker(speaker);
			session.setAttribute("time", null);	
		}
		
		return "redirect:/admin/speaker/manage?pageNum="+pageNum;
	}
	@RequestMapping("/addSpeaker")
	public String addSpeaker(Speaker speaker,HttpSession session,String randomCode) {
		Integer  pageNum = (Integer) session.getAttribute("pageNum");
		Long time = (Long) session.getAttribute("time");
		if(time==null) {
			return "redirect:/admin/course/manage?pageNum="+pageNum;
		}
		if(time==Long.parseLong(randomCode)) {
			int i = ss.addSpeaker(speaker);
			session.setAttribute("time", null);	
		}
		return "redirect:/admin/speaker/manage?pageNum="+pageNum;
	}
	@RequestMapping("/forAddSpeaker")
	public String forAddSpeaker(HttpSession session) {
		Date date = new Date();
		long time = date.getTime();
		session.setAttribute("time", time);
		return "forward:/back/speaker/edit_speaker.jsp";
	}
	@RequestMapping("/deleteSpeaker")
	public String deleteSpeaker(Integer id,HttpSession session) {
		int i = ss.deleteSpeaker(id);
		Integer  pageNum = (Integer) session.getAttribute("pageNum");
		return "redirect:/admin/speaker/manage?pageNum="+pageNum;
	}
}
